//
//  DefaultSportDataEngine.swift
//  LKSportSDKSportManager
//
//  Created by KuaiKuai on 2023/8/25.
//

import Foundation

public class DefaultSportDataEngine: SportDataEngineInterface {
    
   public var currentDatamodel: SportDataModel?
    //模拟腕表数据工具
   public let tool = KKImitateWristWatchOutputTool()
    
    public init() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
            self.makeData()
        }
    }
    
    func makeData() {
         tool.startOutput {[weak self] id, heartRate, sportIntensity, burn in
             self?.currentDatamodel = SportDataModel(id: String(id), heartRate: heartRate, sportIntensity: sportIntensity, burn: burn, extra: nil)
         } stopOutputBlock: {
             
         }
     }
    public func getSportConfig(user: UserConfig) -> SportConfig? {
       let sConfig = SportConfig(id: "1", code: "1001", name: "运动配置01")
        let burnConfig = SportBurnConfig(score: 100000.0/3600, burn: 300)
       sConfig.burnConfig = burnConfig
        
        //空 灰 蓝 黄 绿 橙 红
        let rangeArray: [(Int,Int)] = [(0,1),(2,5),(6,50),(51,100),(101,150),(151, 200),(201, 250)]
        var ranges: [HeartRateRangeConfig] = []
        
        for i in 0 ... (rangeArray.count - 1) {
            let aTuple = rangeArray[i]
            let rangeConfig = ValueRangeConfig(min: aTuple.0, max: aTuple.1)
            var values: [ValueRangeConfig] = []
            values.append(rangeConfig)
            let hrRangeConfig = HeartRateRangeConfig()
            
            if i == HeartRateRangeType.none.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 0
                hrRangeConfig.quality = HeartRateQuality.low.rawValue
            }
            
            if i == HeartRateRangeType.gray.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 0
                hrRangeConfig.quality = HeartRateQuality.low.rawValue
            }
            if i == HeartRateRangeType.blue.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 30.0 * 0.7
                hrRangeConfig.quality = HeartRateQuality.low.rawValue
            }
            if i == HeartRateRangeType.yellow.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 30.0 * 0.8
                hrRangeConfig.quality = HeartRateQuality.low.rawValue
            }
            if i == HeartRateRangeType.green.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 30.0 * 1.0
                hrRangeConfig.quality = HeartRateQuality.nomal.rawValue
            }
            if i == HeartRateRangeType.orange.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 30.0 * 0.9
                hrRangeConfig.quality = HeartRateQuality.nomal.rawValue
            }
            if i == HeartRateRangeType.red.rawValue {
                hrRangeConfig.type = i
                hrRangeConfig.score = 30.0 * 0.5
                hrRangeConfig.quality = HeartRateQuality.Hight.rawValue
            }
             hrRangeConfig.values = values
             
             ranges.append(hrRangeConfig)
        }

        sConfig.heartRateConfig = SportHeartRateConfig(ranges: ranges)
        sConfig.burnConfig = SportBurnConfig(score: 50, burn: 30)
        // 得分区间
        let intensityArray: [(Int,Int)] = [(0,10),(11,35),(36,50)]
        // 强度区间
        let intensityArmlet: [Int] = [0]
        //
        var sportScoreFactor = 0.0
        var intensityranges: [SportIntensityRangeConfig] = []
        // 每差1个区间扣分系数
        let intensityReduce: Double = 0.2
        for index in 0 ... (intensityArray.count) {
            var values: [ValueRangeConfig] = []
            if index == intensityArray.count {
                let aTuple = intensityArray[index - 1]
                let intensityConfig = ValueRangeConfig(min: aTuple.1, max: 9999999)
                values.append(intensityConfig)
            }
            if index < intensityArray.count {
                let aTuple = intensityArray[index]
                let intensityConfig = ValueRangeConfig(min: aTuple.0, max: aTuple.1)
                values.append(intensityConfig)
            }
            if intensityArmlet.contains(index) {
                sportScoreFactor = 1.0
                let intensityRangeConfig = SportIntensityRangeConfig()
                intensityRangeConfig.type = SportIntensityType.perfect.rawValue
                intensityRangeConfig.quality = SportIntensityQuality.perfect.rawValue
                intensityRangeConfig.values = values
                let actionScore = (100000.0/3600) * 1.0 * sportScoreFactor
                intensityRangeConfig.score = actionScore
                intensityranges.append(intensityRangeConfig)
            } else {
                var differenceSection = 0
                if index < intensityArmlet.first ?? 0 {
                    differenceSection = (intensityArmlet.first ?? 0) - index
                } else {
                    differenceSection = index - (intensityArmlet.last ?? 0)
                }
                // 动作得分系数
                sportScoreFactor = 1.0 - Double(differenceSection) * intensityReduce
                if differenceSection == 1 {
                    let intensityRangeConfig = SportIntensityRangeConfig()
                    intensityRangeConfig.type = SportIntensityType.good.rawValue
                    intensityRangeConfig.quality = SportIntensityQuality.good.rawValue
                    intensityRangeConfig.values = values
                    let actionScore = (100000.0/3600) * 1.0 * sportScoreFactor
                    intensityRangeConfig.score = actionScore
                    intensityranges.append(intensityRangeConfig)
                } else {
                    let intensityRangeConfig = SportIntensityRangeConfig()
                    intensityRangeConfig.type = SportIntensityType.bad.rawValue
                    intensityRangeConfig.quality = SportIntensityQuality.bad.rawValue
                    intensityRangeConfig.values = values
                    let actionScore = (100000.0/3600) * 1.0 * sportScoreFactor
                    intensityRangeConfig.score = actionScore
                    intensityranges.append(intensityRangeConfig)
                }
            }
        }
           sConfig.intensityConfig = SportIntensityConfig(ranges: intensityranges)
           return sConfig
    }
    
    public func getSportData(user: UserConfig) -> SportDataModel? {
        return self.currentDatamodel
    }
}












/**
 动作 原来的配置文件逻辑
 for i in 0 ... (intensityArray.count - 1) {
     let aTuple = intensityArray[i]
     let intensityConfig = ValueRangeConfig(min: aTuple.0, max: aTuple.1)
     var values: [ValueRangeConfig] = []
     values.append(intensityConfig)
     let intensityRangeConfig = SportIntensityRangeConfig()
     if i == SportIntensityType.none.rawValue {
         intensityRangeConfig.type = i
         intensityRangeConfig.score = 10
         intensityRangeConfig.quality = SportIntensityQuality.none.rawValue
     }
     if i == SportIntensityType.bad.rawValue {
         intensityRangeConfig.type = i
         intensityRangeConfig.score = 20
         intensityRangeConfig.quality = SportIntensityQuality.bad.rawValue
     }
     if i == SportIntensityType.good.rawValue {
         intensityRangeConfig.type = i
         intensityRangeConfig.score = 30
         intensityRangeConfig.quality = SportIntensityQuality.good.rawValue
     }
     if i == SportIntensityType.perfect.rawValue {
         intensityRangeConfig.type = i
         intensityRangeConfig.score = 40
         intensityRangeConfig.quality = SportIntensityQuality.perfect.rawValue
     }
     intensityRangeConfig.values = values
     intensityranges.append(intensityRangeConfig)
 }
 
 
 */
